// pages/workReport/workReport.js
const app = getApp()
const api = app.globalData.api
Page({

  /**
   * 页面的初始数据
   */
  data: {
    startDate:'',
    endDate:'',
    date_popup_show:false,
    active:0,
    tabIndex:0,
    statistics:[],
    search_type:'',
    commit_time:'',
    type_popup_show:false,
    time_popup_show:false,
    typeOption:[],
    timeOption:[],
    list:[],
    page:1
  },

   /**开始时间选择 */
   bindStartDateChange(e){
    this.setData({
      startDate: e.detail.value
    })
  },
  /**结束时间选择 */
  bindEndDateChange(e){
    this.setData({
      endDate: e.detail.value
    })
  },
  /**自定义时间选择确认 */
  dateSearch(){
    this.setData({
      commit_time:'',
      date_popup_show: false
    })
    if(this.data.tabIndex ==1){
      this.requsetReport('editor')
    }else{
      this.requsetReport('auditor')
    }
  },
/**显示状态选项搜索 */
showPopup() {
  this.setData({
    type_popup_show:true,
  })
},
showTimePopup(){
  this.setData({ time_popup_show: true });
},
  /**汇报类型选项弹出层关闭 */
  onTypeCancel(e){
    this.setData({
      type_popup_show:false
    })
  },
  /**汇报类型搜索选项切换 */
  onTypeConfirm(event){
    const { picker, value, index } = event.detail;
    this.setData({
      search_type:`${value}`,
      type_popup_show:false
    })
    if(this.data.tabIndex ==1){
      this.requsetReport()
    }else{
      this.requsetReport('auditor')
    }   
  },
  /**时间选项弹出层关闭 */
  onTimeCancel(e){
    this.setData({
      time_popup_show:false
    })
  },
  /**时间搜索选项切换 */
  onTimeConfirm(event){
    const { picker, value, index } = event.detail;
    
    if(value=='自定义'){
      this.setData({
        date_popup_show:true,
        time_popup_show:false
      })
    }else{
      this.setData({
        commit_time:`${value}`,
        time_popup_show:false,
        startDate:'',
        endDate:''
      })
      if(this.data.tabIndex ==1){
        this.requsetReport('editor')
      }else{
        this.requsetReport('auditor')
      }
    }
    
  },

  /**我提交的，我收到的 */
  onTabChange(event) {
    let index = `${event.detail.name}`;
    this.setData({
      tabIndex:index,
      search_type:'',
      commit_time:'',
      startDate:'',
      endDate:'',
    })
    if(index==1){
      //我提交的
      this.requsetReport()
    }else{
      //我收到的
      this.requsetReport('auditor')
    }
  },
  /**详情 */
  detail(e){
    let id = e.currentTarget.dataset.id;
    let type = e.currentTarget.dataset.type;
    if(!api.isBlank(id) && !api.isBlank(type)){
      if(type=='日报'){
        wx.navigateTo({
          url: '../dayReportInfo/dayReportInfo?id='+id,
        })
      }else if(type=='周报'){
        wx.navigateTo({
          url: '../weekReportInfo/weekReportInfo?id='+id,
        })
      }else{
        wx.navigateTo({
          url: '../monthReportInfo/monthReportInfo?id='+id,
        })
      }
     
    }else{
      wx.showToast({
        title: '未找到ID或报告类型',
      })
    }
    
  },
  /**请求列表 */
  requsetReport(type='editor',page=1){
    let that = this;
    let para = {
      role:type,
      type:this.data.search_type,
      commit_time:this.data.commit_time,
      startDate:this.data.startDate,
      endDate:this.data.endDate
    }
    api.curlPost('?cla=workReportApp&fun=list&page='+page,para).then((res)=>{
      if(page==1 || !api.isBlank(res)){
        let arr = page==1?res.data.result:that.data.list.concat(res.data.result);
        that.setData({
          list:arr,
          page:Number(res.data.page.pageNow)+1
        })
        wx.stopPullDownRefresh()
      }

    })
  },

/**tabar切换 */
onChange(event){
  let that = this;
  let index = event.detail;
  if(index==0){
    api.curlPost('?cla=workReportApp&fun=home').then((res)=>{
      that.setData({
        active: index,
        menu:res.data.result
      })
    })
  }else if(index==1){//查看
    that.setData({
      active: index,
      search_type:'',
      commit_time:'',
      startDate:'',
      endDate:'',
    })
    this.requsetReport('auditor')
  }else if(index==2){
    api.curlPost('?cla=workReportApp&fun=statistics').then((res)=>{
      that.setData({
        active: index,
        statistics:res.data.result
      })
    })
  }
  that.setData({
    active: index
  })
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    api.curlPost('?cla=workReportApp&fun=home').then((res)=>{
      that.setData({
        menu:res.data.result,
      })
    })
    api.curlPost('?cla=workReportApp&fun=search').then((res)=>{
      that.setData({
        timeOption:res.data.result.timeOption,
        typeOption:res.data.result.typeOption,
      })
    })
  },

  onSwipeClose(event){
    let that = this;
    const { position, instance } = event.detail;
    switch (position) {
      case 'right':
        wx.showModal({
          title: '提示',
          content: '确定删除吗？',
          success (res) {
            if (res.confirm) {
              // console.log('用户点击确定'+event.currentTarget.dataset.id)
              api.curlPost('?cla=workReportApp&fun=delete',{id:event.currentTarget.dataset.id}).then((res)=>{
                that.deleteData(event.currentTarget.dataset.id)
                wx.showToast({
                  title: res.warn,
                })
              })
             
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          },
          complete(){
            instance.close();
          }
        })
        break;
    }
  },
  
  /**删除数据 */
  deleteData(id){
    let list = this.data.list;
    let newList = list.filter(item => item.id !=id );
    this.setData({
      list:newList
    })
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    if(this.data.tabIndex ==1){
      this.requsetReport('editor')
    }else{
      this.requsetReport('auditor')
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.tabIndex ==1){
      this.requsetReport('editor',this.data.page)
    }else{
      this.requsetReport('auditor',this.data.page)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})